package com.huajx.tuopuda.client.udp;

import java.awt.Stroke;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import com.huajx.util.StrUtil;

/**
 * 读取/写入已读取的记录条数
 * @author Administrator
 *
 */
public class ReadWriteRecord {
	/**
	 * 写入已读取的记录条数
	 * @param sn
	 * @param record
	 * @throws IOException 
	 */
	public static void write(int sn,int record) throws IOException{
		File writeFile= new File(sn + "-on.txt");
		if(!writeFile.exists()){
			writeFile.createNewFile();
		}
	    FileWriter fw = new FileWriter(writeFile.getAbsoluteFile());
	    BufferedWriter bw = new BufferedWriter(fw);
	    bw.write(String.valueOf(record));
	    bw.close();
	}
	/**
	 * 文件读取流
	 * @param sn
	 * @throws IOException
	 */
	public static int read(int sn) throws IOException{
		File readFile= new File(sn + "-on.txt");
		if(!readFile.exists()){
			readFile.createNewFile();
		}
		FileReader reader = new FileReader(readFile);
		BufferedReader br = new BufferedReader(reader); // 建立一个对象，它把文件内容转成计算机能读懂的语言
		String line;
	    //网友推荐更加简洁的写法
	    while ((line = br.readLine()) != null) {
	        // 一次读入一行数据
	    	break;
	    }
	    if(StrUtil.isBlank(line)){
	    	return 0;
	    }
		return Integer.parseInt(line.trim());
	}
	/*public static void main(String[] args) throws IOException {
		try {
			write("123456", 11223);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int read = read("123456");
		System.out.println(read);
	}*/

}
